<?php

class Ticket extends Goods {
	
	/**
	 * 出发时间
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $departure_time;

	/**
	 * 出发城市
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $departure_city;
	/**
	 * 到达城市
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $landing_city;
	
	public $accommodation;
	
	public $city = array(
		'departure' => array(
			'departure_province',
			'departure_city',
		),
		'arrival' => array(
			'arrival_province',
			'arrival_city',
		)
	);
	
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function viewUrl($time = '') {
		$time = empty($time) ? date('Y-m-d') : $time;
		return Yii::app()->createUrl('provide/channel/ticketList',array(
			'Ticket[departure_city]' => CHtml::value($this, 'profile.departure_province'),
			'Ticket[landing_city]' => CHtml::value($this, 'profile.arrival_province'),
			'Ticket[departure_time]' => $time,
		));
	}
	
	/**
	 * 机票获取最小价格
	 * Enter description here ...
	 * @param unknown_type $dCity 出发城市
	 * @param unknown_type $lCity 到达城市
	 * @param unknown_type $time 时间
	 * @param unknown_type $attribute 显示字段
	 */
	public function ticketMinPrice($dCity,$lCity,$time = '', $attribute = 'price_dealer') {
		$model = new VGoodsPrice();
		$criteria = new CDbCriteria();
		$criteria->select = "t.*";
		$criteria->join = "LEFT JOIN goods_ticket AS r ON t.goods_id = r.goods_id";
		$criteria->addCondition('r.goods_id IS NOT NULL');
		if($time)
			$criteria->addCondition("(datetime IS NULL OR datetime LIKE'%{$time}%')");
		if ($lCity)
			$criteria->compare($this->cityAttribute('arrival', $lCity), $lCity);
		if ($lCity)
			$criteria->compare($this->cityAttribute('departure', $dCity), $dCity);
		$criteria->order = "$attribute ASC";
		$obj = $model->find($criteria);
		return is_object($obj) ? $obj->$attribute : 0;
	}
	
	public function cityAttribute($type,$id) {
		$city = CommonDistrict::model()->findByPk($id);
		$key = $city->layer - 1;
		return !empty($this->city[$type][$key]) ? $this->city[$type][$key] : $this->city[$type][0] ;
	}
}

?>